<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\tree\util\oitem;

/**
 * 模态框
 * @author cleverstone
 * @since ym2.0
 */
class Modal extends OperationType
{
    /**
     * @var string 位置, 只有:top(上边), bottom(下边)
     */
    private $_position = 'top';

    /**
     * @var string 按钮标题
     */
    private $_title = 'modal';

    /**
     * @var string 按钮图标
     */
    private $_icon = '';

    /**
     * @var string 按钮class样式
     */
    private $_btnClass = 'layui-btn-theme';

    /**
     * @var array 选项
     */
    private $_options = [
        'type' => 'modal',  // 支持的类型有: page(页面), modal(模态框), ajax(AJAX)
        'params' => [],     // 请求参数, 默认空
        'route' => '',      // 路由; 默认当前路由
        'width' => '620px', // 当前type为modal时有效, 指定modal的宽;
        'height' => '750px',// 当前type为modal时有效, 指定modal的高;
        'closeBtn' => false,// 底部关闭按钮
        '__mustSelectCheckbox' => false, // 是否必须选择checkbox
    ];

    /**
     * @param array|string $btnClass 按钮class样式
     * @return $this
     */
    public function btnClass($btnClass)
    {
        $this->_btnClass = $btnClass;
        return $this;
    }

    /**
     * 位于底部
     * @return $this
     */
    public function positionBottom()
    {
        $this->_position = 'bottom';
        return $this;
    }

    /**
     * @param string $title 名称标题
     * ```php
     * // 文本标题
     * $this->title('新增');
     *
     * // 支持HTML
     * $this->title(H5::text('新增'));
     *
     * ```
     *
     * @return $this
     */
    public function title($title)
    {
        $this->_title = (string)$title;
        return $this;
    }

    /**
     * @param string $icon 按钮图标
     * 支持的图标有:
     * - bootstrap v4图标
     * - fontawesome v5
     * - layui 2.8图标
     *
     * @return $this
     */
    public function icon($icon)
    {
        $this->_icon = (string)$icon;
        return $this;
    }

    /**
     * @param string|array $params 请求参数
     * one:
     * ```php
     * // 直接指定query中的字段
     * $this->params(['id']);
     *
     * ```
     *
     * two:
     * ```php
     * // key-value, 字段指定值
     * $this->params(['id' => 1]);
     *
     * ```
     *
     * three:
     * ```php
     * // key-占位符, 字段+字段占位符
     * $this->params(['action' => ':status']);
     *
     * ```
     * @return $this
     */
    public function params($params)
    {
        $params = (array)$params;
        foreach ($params as $key => $value) {
            if (is_int($key) || (is_string($value) && substr($value, 0, 1) == ':')) {
                $this->_options['__mustSelectCheckbox'] = true;
            }
        }

        $this->_options['params'] = $params;
        return $this;
    }

    /**
     * @param string $route 路由或外链
     * ```php
     * 可以直接指定路由
     * $this->route('user/add');
     *
     * 也可以指定外部链接
     * $this->route('https://ym2.cleverstone.top');
     *
     * ```
     *
     * @return $this
     */
    public function route($route)
    {
        $this->_options['route'] = $route;
        return $this;
    }

    /**
     * @param string $width 模态框的宽, 当type为modal时有效;
     * @return $this
     */
    public function width($width = '100%')
    {
        if (is_numeric($width)) {
            $width .= 'px';
        }

        $this->_options['width'] = $width;
        return $this;
    }

    /**
     * @param string $height 模态框的高, 当type为modal时有效;
     * @return $this
     */
    public function height($height = '100%')
    {
        if (is_numeric($height)) {
            $height .= 'px';
        }

        $this->_options['height'] = $height;
        return $this;
    }

    /**
     * 显示底部关闭按钮
     * @return $this
     */
    public function closeBtn()
    {
        $this->_options['closeBtn'] = true;
        return $this;
    }

    /**
     * @return array
     */
    public function _toArray()
    {
        if (!empty($this->_options['route'])) {
            if (!$this->checkAccess($this->_options['route'])) {
                return [];
            }
        }

        return [
            'position' => $this->_position,
            'title' => $this->_title,
            'icon' => $this->_icon,
            'btnClass' => $this->_btnClass,
            'options' => $this->_options,
        ];
    }
}